//
//  MainTabBarController.swift
//  SwiftProject
//
//  Created by wwj on 16/2/23.
//  Copyright © 2016年 wwj. All rights reserved.
//

import UIKit

class TabBarController: UITabBarController, TabBarViewDelegate {
    var tabBarView: TabBarView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initTabBar()
        initAllViewController()
       
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        for view:UIView in self.tabBar.subviews{
            if (view.isKindOfClass(UIControl)){
                view.removeFromSuperview()
            }
        }
    }
    
    //MARK: 自定义TabBar
    private func initTabBar(){
        tabBarView = TabBarView()
        tabBarView.delegate = self
        tabBarView.frame = self.tabBar.bounds
        self.tabBar.addSubview(tabBarView)
    }
    
    func onClickTabBarView(tabBarView: TabBarView, index: NSInteger) {
        self.selectedIndex = index
    }
    
    private func initAllViewController(){
        initChickViewController(HomeViewController(), name: "首页", imageName: "v2_home", selectImageName: "v2_home_r")
        initChickViewController(MarketViewController(), name: "闪电超市", imageName: "v2_order", selectImageName: "v2_order_r")
        initChickViewController(CartViewController(), name: "购物车", imageName: "shopCart", selectImageName: "shopCart_r")
        initChickViewController(MeViewController(), name: "我的", imageName: "v2_my", selectImageName: "v2_my_r")
    }

    private func initChickViewController(childVC:UIViewController, name:String, imageName:String, selectImageName:String){
        let naviVC = UINavigationController(rootViewController: childVC)
        addChildViewController(naviVC)
        tabBarView.addTabBarButtonWith(name, image: imageName, selectImage: selectImageName)
    }


}
